/*********************************************************************************
 *      Copyright:  (C) 2022 zengwen<1825984037@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  get_massage.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(07/07/22)
 *         Author:  zengwen <1825984037@qq.com>
 *      ChangeLog:  1, Release initial version on "07/07/22 14:22:17"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "logger.h"

#define SHT20_PATH "/dev/sht20"

int get_temp_hum(float *temp_f,float *hum_f)
{
    int             ret=0;
    int             fd_sht20=0;
    unsigned short  temp,hum;
    unsigned short  databuf[2]={0};

	if(!temp_f||!hum_f)
	{
		log_err("the temp or hum argument is failure!\r\n");
        return -1;
	}

    fd_sht20 = open(SHT20_PATH, O_RDWR);

    if(fd_sht20 < 0)
    {
        log_err("open sht20 device file fail\r\n");
        close(fd_sht20);
        return -1;
    }

    ret = read(fd_sht20,&databuf,sizeof(databuf));

    if(ret==0)//数据读取成功
    {
        temp = databuf[0];
        hum = databuf[1];

        *temp_f = -46.85 + (175.72 * (temp / (float) 0xFFFF));
        *hum_f = -6.0 + (125.0 * (hum / (float) 0xFFFF));
    }

	else
    {
        log_err("read temperature and humidity failed!\r\n");//按下
        close(fd_sht20);
        return -1;
    }

    close(fd_sht20);
    return 0;
}

/*get time*/
int get_time(char cur_time[])
{
	time_t timep;
	struct tm *p;

	time(&timep);
	p = gmtime(&timep);
	snprintf(cur_time, 20, "%d.%d.%d %d:%d:%d", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, 8 + p->tm_hour, p->tm_min, p->tm_sec);
	return 0;
}
